//
// Created by 谢哲 on 2021/5/18.
//

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

class Edge {
public:
    vector<int> edges;
    int index = 0;
    int rudu = 0, chudu = 0;
    Edge() = default;
    Edge(vector<int> edges, int rudu, int chudu, int index): edges(std::move(edges)), rudu(rudu), chudu(chudu), index(index) {}
};

int check_circle(vector<Edge> edges, int n) {
    queue<Edge> qdata;
    for (auto edge: edges) {
        if (edge.rudu==0) qdata.push(edge);
    }

    int cnt = 0;
    while(!qdata.empty()) {
        auto top = qdata.front();
        qdata.pop();
        cnt += 1;
        for (auto edge: top.edges) {
            --edges[edge].rudu;
            if (edges[edge].rudu==0) {
                qdata.push(edges[edge]);
            }
        }
    }

    if (cnt<n) return 1;

    return 0;
}

// 0 ok, 1 not yet
vector<int> ans;
int check(vector<Edge> edges, int n) {
    ans.clear();

    queue<Edge> qdata;

    for (auto edge: edges) {
        if (edge.rudu==0) qdata.push(edge);
    }

    while(!qdata.empty()) {
        if(qdata.size()>1) return 1;
        auto top = qdata.front();
        qdata.pop();
        ans.emplace_back(top.index);
        for (auto edge: top.edges) {
            --edges[edge].rudu;
            if (edges[edge].rudu==0) {
                qdata.push(edges[edge]);
            }
        }
    }

    return 0;
}

int main() {
    int n, m;
    cin >> n >> m;

    vector<Edge> edges(n);
    for (int i=0;i<n;++i) edges[i].index = i;

    for (int i=1;i<=m;++i) {
        string s;
        cin >> s;
        int p1 = s[0]-'A', p2 = s[2]-'A';
        edges[p1].edges.emplace_back(p2);
        edges[p1].chudu++;
        edges[p2].rudu++;

        // Check
        int circle_result = check_circle(edges, n);
        if (circle_result==1) {
            cout << "Inconsistency found after "<<i<<" relations.";
            return 0;
        }
        int result = check(edges, n);
        if (result==0) {
            cout << "Sorted sequence determined after "<<i<<" relations: ";
            for (auto p:ans) cout << char('A'+p);
            cout << ".";
            return 0;
        }
    }

    cout << "Sorted sequence cannot be determined.";

    return 0;
}
